// +----------------------------------------------------------------------
// | FileName:   FlowLogService.java  
// +----------------------------------------------------------------------
// | Copyright:  http://www.xueyong.net.cn
// +----------------------------------------------------------------------
// | Author:     xab(admin@xueyong.net.cn) 
// | 			 Auto Create Java File 
// +----------------------------------------------------------------------
package com.lac.xaboa.db.service;

import android.content.Context;

import java.util.List;

import com.lac.xaboa.db.DBUtils;
import com.lac.xaboa.db.DaoSession;
import com.lac.xaboa.db.model.FlowLog;
import com.lac.xaboa.db.dao.FlowLogDao;
import com.lac.xaboa.db.dao.FlowLogDao.Properties;

import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

public class FlowLogService {

    private static Context mContext;
    private static FlowLogService instance;

    private FlowLogDao dao;

    private FlowLogService() {
    }

    public static FlowLogService getInstance(Context context) {
        if (instance == null) {
            instance = new FlowLogService();
            if (mContext == null) {
                mContext = context;
            }
            // 数据库对象
            DaoSession daoSession = DBUtils.getDaoSession(mContext);
            instance.dao = daoSession.getFlowLogDao();
        }
        return instance;
    }

    /**
     * 添加数据
     */
    public void add(FlowLog flowLog) {
        dao.insert(flowLog);
    }

    /**
     * 添加数据数组
     */
    public void addList(List<FlowLog> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            FlowLog flowLog = list.get(i);
            dao.insertOrReplace(flowLog);
        }

    }

    /**
     * 删除 根据ID
     */
    public void _delById(int id) {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        DeleteQuery<FlowLog> bd = qb.where(Properties._id.eq(id)).buildDelete();
        bd.executeDeleteWithoutDetachingEntities();
    }

    /**
     * 删除所有
     */
    public void delAll() {
        dao.deleteAll();
    }

    /**
     * 修改数据
     */
    public void update(FlowLog flowLog) {
        dao.insertOrReplace(flowLog);
    }

    /**
     * 查询所有
     */
    public List<FlowLog> getAll() {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        return qb.list();
    }

    /**
     * 数据是否存在
     */
    public boolean isExist(int id) {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        qb.where(Properties._id.eq(id));
        qb.buildCount().count();
        return qb.buildCount().count() > 0 ;
    }

    /**
     * 数据是否存在
     */
    public boolean isExistById(String id) {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        qb.where(Properties.Id.eq(id));
        qb.buildCount().count();
        return qb.buildCount().count() > 0 ;
    }

    /**
     * 单个数据
     */
    public FlowLog displayById(String id) {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        qb.where(Properties.Id.eq(id));
        return qb.unique();
    }

    /**
     * 删除单挑数据根据ID
     */
    public void delById(String id) {
        QueryBuilder<FlowLog> qb = dao.queryBuilder();
        DeleteQuery<FlowLog> bd = qb.where(Properties.Id.eq(id)).buildDelete();
        bd.executeDeleteWithoutDetachingEntities();
    }

    /**
     * 服务器同步数据单条
     */
    public void syncModel(FlowLog flowLog) {
        delById(flowLog.getId());
        add(flowLog);
    }

    /**
     * 服务器同步数据多条
     */
    public void syncList(List<FlowLog> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            FlowLog flowLog = list.get(i);
            syncModel(flowLog);
        }
    }

    // +----------------------------------------------------------------------
    // | 以后的方法属于手动添加，如果需要重新生成，请先备份
    // +----------------------------------------------------------------------
}